class Solution {
public:
    TreeNode* Build(vector<int>& inorder, vector<int>& postorder, int& endi,
                    int begin, int end) {
                        if(begin>end)
                        return nullptr;
        // 根据后序确定根
        TreeNode* root = new TreeNode(postorder[endi]);
        // 通过中序划分区间
        int rooti = begin;
        while (rooti < end && inorder[rooti] != root->val) {
            rooti++;
        }
        endi--;
        root->right = Build(inorder, postorder, endi, rooti + 1, end);
        root->left = Build(inorder, postorder, endi, begin, rooti - 1);
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int endi = postorder.size() - 1;
       return Build(inorder, postorder, endi, 0, inorder.size() - 1);
    }
};